package com.summer.mall.service.impl;

import com.summer.mall.common.ResponseService;
import com.summer.mall.entity.Category;
import com.summer.mall.mapper.CategoryMapper;
import com.summer.mall.service.CategoryService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public ResponseService<String> addCategory(String categooryName, Integer parentId) {
        //若商品分类没有父类Id，则代表商品还未归类，故商品种类参数错误
        if(StringUtils.isNotBlank(categooryName) || parentId == null){
            return ResponseService.createByErrorMessage("商品种类参数错误");
        }

        Category category = new Category();
        category.setName(categooryName);
        category.setParentId(parentId);
        category.setStatus(true);//表示这个分类是可用的
        int count = categoryMapper.insert(category);
        if (count > 0 ){
            return ResponseService.createBySuccessMessage("添加商品品类成功");
        }
        return ResponseService.createByErrorMessage("添加失败");

    }


    @Override
    public ResponseService updateCategory(String categoryName, Integer categoryId) {
        if (StringUtils.isBlank(categoryName) || categoryId == null){
            return ResponseService.createByErrorMessage("商品品类参数错误");
        }
        Category category = new Category();
        category.setName(categoryName);
        category.setId(categoryId);
        int resultCount = categoryMapper.updateByPrimaryKey(category);
        if(resultCount > 0){
            return ResponseService.createBySuccessMessage("更新品类信息成功");
        }
        return ResponseService.createByErrorMessage("更新品类名字失败");
    }

    @Override
    public ResponseService<List<Category>> getChildrenParallelCategory(Integer parentId) {
        List<Category> list = categoryMapper.selectChildrenParallelCategoryByParentId(parentId);
        //判断本身是不是空,换判断是不是一个空集合
        if(CollectionUtils.isEmpty(list)){
            return ResponseService.createByErrorMessage("未找到当前分类的子分类");
        }
        return ResponseService.createBySuccess(list);

    }


    /**
     * @author complone
     * @Description 递归查询本节点和孩子节点ID
     * @Param 查询作为父节点的节点ID
     * @Date 9:43 2018/7/9
     * @return
     **/
    @Override
    public ResponseService<List<Integer>> selectCategoryAndChildrenById(Integer categoryId) {
        Set<Category> categorySet = new HashSet<>();
        findChildrenCategory(categorySet,categoryId);
        List<Integer> categoryIdList = new ArrayList<>();//记录该节点ID
        for (Category e:categorySet){
            categoryIdList.add(e.getId());
        }

        return ResponseService.createBySuccess(categoryIdList);

    }

    /**
     * @author complone
     * @Description 递归所有子节点，用Set集合去重,查询已有的子节点进行添加
     * @Param 待添加的节点集合
     * @Param 需要查找的该分类所有子节点的父节点，逐级更新
     * @Date 9:32 2018/7/9
     * @return
     **/
    public Set<Category> findChildrenCategory(Set<Category> categorySet,Integer categoryId){
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        if(category != null){
            categorySet.add(category);
            List<Category> categoryList = categoryMapper.selectChildrenParallelCategoryByParentId(categoryId);
            for(Category item:categoryList){
                findChildrenCategory(categorySet,item.getId());
            }
        }
        return categorySet;
    }



}
